<?php
/**
 * Author: xx.com
 * Date: 2015-12-21
 */

namespace Admin\Controller;

use Common\Logic\ImLogic;

class ImController extends BaseController{
	
	protected $logic;

	public function __construct()
	{
		parent::__construct();
		$this->logic = new ImLogic;
	}

	// 群列表
	public function group()
	{ 		
		$this->display();
	}

	// 群列表
	public function grouplist()
	{
		$condition = array();
        I('name') ? $condition['name'] = I('name') : false;
		I('desc') ? $condition['desc'] = I('desc') : false;
		
		$sort_order = I('order_by','create_time').' '.I('sort','desc');
        
        $model = $this->logic->GroupModel;
        $count = $model->where($condition)->count();
        $Page  = new \Think\AjaxPage($count,10);
        //  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
        
		$list = $model->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();

		$userModel = M('users');

		foreach($list as $k => &$v)
		{
			$v['owner_name'] = $userModel->cache(300)->where([
				'user_id' => $v['owner']
			])->getField('nickname');
			$v['create_time'] = date('Y-m-d',$v['create_time']);
		}

        $show = $Page->show();
		$this->assign('list',$list);		
		$this->assign('page',$Page->show());		
		$this->display();
	}

	// 群详情
	public function groupdetail()
	{
		$groupid = I('groupid');

		if( $groupid )
		{ 

			$info = $this->logic->Easemob->getGroupDetail([$groupid]);
			if( $info )
			{
				// p($info);
				$info = $info[0];
				$group = $this->logic->GroupModel->where([
					'groupid' => $groupid
				])->find();

				if( $group )
				{
					$info = array_merge($group, $info);
					// p($info);					
				}
				$this->assign('info', $info);
				return $this->display();
			}
			return $this->error('无法获取信息');
		}
		return $this->error('参数错误');
	}

	public function groupusers()
	{
		$groupid = I('groupid');
		$page = I('p', 1);

		$list = $this->logic->getGroupMembersArray([
			'groupid' => $groupid
		]);

		$Page  = new \Think\AjaxPage(count($list),100);

		$array = \array_chunk($list, 100);
		
		$users = isset($array[$page-1]) ? $array[$page-1] : '';

		$userModel = M('users');

		$list = [];

		foreach($users as $v)
		{
			$list[] = $userModel->cache(300)->where([
				'user_id' => $v
			])->field('user_id,nickname,head_pic')->find();
		}

        $show = $Page->show();
		$this->assign('groupid',$groupid);
		$this->assign('list',$list);
		$this->assign('page',$Page->show());		
		$this->display();
	}

	// 解散群
	public function destory_group()
	{
		$groupid = I('groupid');

		if( $groupid )
		{
			M()->startTrans();

			$del = $this->logic->GroupModel->where([
				'groupid' => $groupid
			])->delete();

			if( $del )
			{
				$res = $this->logic->Easemob->deleteGroup($groupid);

				if( $res )
				{
					M()->commit();
					return $this->success('操作成功');
				}
				M()->rollback();
				return $this->error('操作远程失败');
			}
			M()->rollback();
			return $this->error('本地操作失败');
		}
		M()->rollback();
		return $this->error('操作失败');
	}

	// 踢人
	public function quit_group()
	{
		$groupid = I('groupid');
		$user_id = I('user_id');

		if( $groupid && $user_id )
		{ 

			$del = $this->logic->quitGroup([
				'groupid' => $groupid,
				'user_id' => $user_id,
			]);

			if( $del && $del['status'] == 1 )
			{				 
				return $this->success('操作成功');
			}
		}
		return $this->error('操作失败');
	}

	// 全平台邀请授权
	public function owner_auth()
	{
		$groupid = I('groupid');
		$owner_auth = I('owner_auth', 0);

		if( $groupid )
		{ 

			$update = $this->logic->GroupModel->where([
				'groupid' => $groupid,
			])->save([
				'owner_auth' => $owner_auth,
				'update_time' => time()
			]);

			if( $update !== false)
			{				 
				return $this->success('操作成功');
			}
		}
		return $this->error('操作失败');
	}

	// 全员禁言
	public function can_speak()
	{
		$groupid = I('groupid');
		$can_speak = I('can_speak', 0);

		if( $groupid )
		{ 

			$update = $this->logic->can_speak_switch($groupid, $can_speak);

			if( $update )
			{				 
				return $this->success('操作成功');
			}
		}
		return $this->error('操作失败');
	}
}